﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;

namespace prjCompras
{
    public partial class frmPesquisaPadrao : Form
    {
        public frmPesquisaPadrao()
        {
            InitializeComponent();
        }

        private List<String> tabelas = new List<string>();
        private List<String> campos = new List<string>();
        private List<String> titulos = new List<string>();
        public bool cancelar { get; set; }
        public string pk { get; set; }

        public void setTabela(string tabela)
        {
            this.tabelas.Add(tabela);
        }

        public void addCampo(string campo)
        {
            this.campos.Add(campo);
        }

        public void addTitulo(string titulo)
        {
            this.titulos.Add(titulo);
        }

        public void setTituloForm(string titulo)
        {
            this.Text = titulo;
        }

        public void fecharForm()
        {
            this.Dispose();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (dtgrdvwPesquisa.RowCount > 0)
            {
                pk = dtgrdvwPesquisa.CurrentRow.Cells[0].Value.ToString();
                this.Close();
            }
            else
            {
                cancelar = true;
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            fecharForm();
            cancelar = true;
        }

        private void frmPesquisaPadrao_Shown(object sender, EventArgs e)
        {
            cancelar = false;
            pk = "";
        }

        public void carregaCombo()
        {
            foreach (string c in campos)
            {
                cmbxCampos.Items.Add(c);
            }
            cmbxCampos.SelectedIndex = 0;
        }

        public void carregaGrid()
        {
            DataTable dt;
            if (cmbxCampos.Text.Equals(""))
            {
                dt = Conexao.getInstance.executeQuery(SqlCrud.getInstance.select(this.tabelas, this.campos));
            }
            else
            {
                dt = Conexao.getInstance.executeQuery(SqlCrud.getInstance.selectLike(this.tabelas, cmbxCampos.Text, this.campos, txtbxValorPesquisa.Text + "%"));
            }
            dtgrdvwPesquisa.DataSource = dt;
            nomeiaColunas();
        }

        private void nomeiaColunas()
        {
            int i = 0;
            foreach (string t in titulos)
            {
                dtgrdvwPesquisa.Columns[i].HeaderText = t;
                i++;
            }
        }

        private void btnPesquisar_Click(object sender, EventArgs e)
        {
            carregaGrid();
        }

        private void frmPesquisaPadrao_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private void dtgrdvwPesquisa_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dtgrdvwPesquisa.RowCount > 0)
            {
                pk = dtgrdvwPesquisa.CurrentRow.Cells[0].Value.ToString();
                this.Close();
            }
        }
    }
}
